﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using FireBillBook.Core.Models;
using FireBillBook.Core.Models.OriginalDatas;
using FireBillBook.Core.Services.Interfaces;
using System.Linq;
using Microsoft.Extensions.Options;
using FireBillBook.Core.Models.Options;

namespace FireBillBook.Core.Services
{
    public class PlatformServiceFactory : IPlatformServiceFactory
    {
        private IOptionsMonitor<PlatformServiceFactoryOptions> _optionsMonitor { get; }
        private IServiceProvider _services { get; set; }
        public PlatformServiceFactory(IServiceProvider serviceProvider, IOptionsMonitor<PlatformServiceFactoryOptions> optionsMonitor)
        {
            _services = serviceProvider;
            _optionsMonitor = optionsMonitor;
        }

        public Dictionary<string, Func<IServiceProvider, BasePlatformService>> builders { get; set; }

        public BasePlatformService GetPlatform(string platform)
        {
            var platformOptions = _optionsMonitor.Get(platform);

            if(platformOptions.initPlatform == null)
            {
                throw new ArgumentNullException(nameof(platformOptions.initPlatform));
            }

            var platformService = platformOptions.initPlatform(_services);

            return platformService;
        }
    }
}
